#Version 2.1

from decimal import *
from tkinter import *
from tkinter import ttk
from tkinter import font
import time, _thread, subprocess

def pcsound():
	return_code = subprocess.call(["afplay", "playerchange.wav"])
def pssound():
	return_code = subprocess.call(["afplay", "pause.m4a"])
def tcsound():
	return_code = subprocess.call(["afplay", "ticktock.wav"])
def gosound():
	return_code = subprocess.call(["afplay", "gameover.wav"])
def unsound():
	return_code = subprocess.call(["afplay", "undo.wav"])


class Chclock(ttk.Frame):

	@classmethod
	def main(cls):
		global root
		root = Tk()
		app = cls(root)
		app.grid(sticky=NSEW)
		root.title('Chessclock')
		root.geometry('750x250-5+40')
		root.minsize(700,200)
		root.grid_columnconfigure(0, weight=1)
		root.grid_rowconfigure(0, weight=1)
		root.resizable(True, False)
		root.mainloop()

	def __init__(self, root):
		super().__init__(root)
		root.bind('q', self.player2move)
		root.bind('p', self.player1move)
		root.bind('b', self.pause)
		root.bind('a', self.undo)
		root.bind('l', self.undo)
		self.create_variables()
		self.create_widgets()
		self.grid_widgets()
		self.grid_columnconfigure(0, weight=1)

	def create_variables(self):
		self.player1 = StringVar(self, '0000')
		self.player2 = StringVar(self, '0000')
		self.player1z = StringVar(self)
		self.player2z = StringVar(self)
		self.timer = StringVar(self)
		self.running = BooleanVar(self)
		self.player1ismoving = BooleanVar(self)

	def create_widgets(self):
		self.set_timer = ttk.Entry(self, width=8, textvariable=self.timer)
		self.start = ttk.Button(self, text='Start', command=self.start)
		self.descr1 = ttk.Label(self, text='Time limit:')
		self.descr2 = ttk.Label(self, text='seconds.')
		TimeFont = font.Font(family='Palatino', size=92, weight='bold')
		self.display1 = ttk.Label(self, textvariable=self.player1, font=TimeFont)
		self.display2 = ttk.Label(self, textvariable=self.player2, font=TimeFont)
		self.quit = ttk.Button(self, text='Quit', command=self.quit)

	def grid_widgets(self):
		self.set_timer.grid(column=1, row=0, sticky=(N,S), pady=10)
		self.start.grid(column=1, row=1, sticky=(N,S), pady=10)
		self.descr1.grid(column=0, row=0, sticky=(E))
		self.descr2.grid(column=2, row=0, sticky=(W))
		self.display1.grid(column=0, row=2, sticky=(W), padx=15)
		self.display2.grid(column=2, row=2, sticky=(E), padx=15)
		self.quit.grid(column=1, row=3, sticky=(N,S), pady=10)
		self.columnconfigure(0, weight=1)
		self.columnconfigure(1, weight=0)
		self.columnconfigure(2, weight=1)


	def start(self):
		timer = self.timer.get()
		self.player1.set(Decimal(self.timer.get()))
		self.display1.config(foreground='black')
		self.player2.set(Decimal(self.timer.get()))
		self.display2.config(foreground='black')
		getcontext().prec = (len(self.player1.get())+1)
		_thread.start_new_thread(tcsound, ())
		self.running.set(0)
	
	def player1move(self, *args):
		x=Decimal(self.player1.get())
		self.player1z.set(x)
		_thread.start_new_thread(pcsound, ())
		self.running.set(1)
		self.player1ismoving.set(1)
		while x>30.1 and self.running.get()==1:
			x=x-Decimal(0.1)
			self.player1.set(Decimal(x).quantize(Decimal('.1'), rounding=ROUND_DOWN))
			self.update()
			time.sleep(0.1)
			if (x%60)==0:
				_thread.start_new_thread(tcsound, ())
		
		while x>0 and self.running.get()==1:
			self.display1.config(foreground='red')
			x=x-Decimal(0.1)
			self.player1.set(Decimal(x).quantize(Decimal('.1'), rounding=ROUND_DOWN))
			self.update()
			if int(x)==Decimal(x) and not int(x)==0:
				_thread.start_new_thread(tcsound, ())
			time.sleep(0.1)
		if int(x)==0:
			self.player1.set(0)
			_thread.start_new_thread(gosound, ())
			
	def player2move(self, *args):
		x=Decimal(self.player2.get())
		self.player2z.set(x)
		_thread.start_new_thread(pcsound, ())
		self.running.set(1)
		self.player1ismoving.set(0)
		while x>30.1 and self.running.get()==1:
			x=x-Decimal(0.1)
			self.player2.set(Decimal(x).quantize(Decimal('.1'), rounding=ROUND_DOWN))
			self.update()
			time.sleep(0.1)
			if (x%60)==0:
				_thread.start_new_thread(tcsound, ())

		while x>0 and self.running.get()==1:
			self.display2.config(foreground='red')
			x=x-Decimal(0.1)
			self.player2.set(Decimal(x).quantize(Decimal('.1'), rounding=ROUND_DOWN))
			self.update()
			if int(x)==Decimal(x) and not int(x)==0:
				_thread.start_new_thread(tcsound, ())
			time.sleep(0.1)
		if int(x)==0:
			self.player2.set(0)
			_thread.start_new_thread(gosound, ())
			
	def pause(self, *args):
		_thread.start_new_thread(pssound, ())
		z=self.running.get()
		if z==0:
			z=1
		else:
			z=0
		self.running.set(z)
		
	def undo(self, *args):
		_thread.start_new_thread(unsound, ())
		if self.player1ismoving.get()==1:
			self.player1.set(self.player1z.get())
			self.player2move()
		else:
			self.player2.set(self.player2z.get())
			self.player1move()
		self.update()
	
	def quit(self):
		self.pause()
		root.destroy()

if __name__ == '__main__':
	Chclock.main()